package htjs.site.web.engine.extract;
 
 
import htjs.site.service.bo.gen.engine.extract.*;
import htjs.site.service.bo.gen.engine.write.SimpleIndexWriter;
import htjs.site.service.domain.tools.BeansHelp;
 
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
 
public class Page {
    private int start = 1;//页面的起始位置
    private int size = 100;//每页显示的记录数
    private int end = 5;//页面的终止位置
    private int count = 0;//数据库中的记录数
    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    private htjs.site.service.bo.gen.engine.extract.JDBCUtil jd;//数据库连接的工具包
 
    public void set(int start, int end) {
        this.start = start;
        this.end = end;
    }
 
    public Page() {
        jd = new htjs.site.service.bo.gen.engine.extract.JDBCUtil();
        con = jd.getCon();
    }
 
 
    public int getCountOfWD() {
        String sqlCount = "select count(*) from ct_document";
        try {
            ps = con.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
 
    public List getNextPage() {
 
        String sql = "select doc_id,create_time,edit_time,author_id,edit_id,title,status,content,type_id,abstract,rnum from(select doc_id,create_time,edit_time,author_id,edit_id,title,status,content,type_id,abstract,rownum rnum from ct_document)" +
                "where rnum between ? and ?";
 
        List wdList = new ArrayList();
        try {
 
            ps = con.prepareStatement(sql);
            ps.setInt(1, start);
            ps.setInt(2, end);
 
            rs = ps.executeQuery();
            while (rs.next()) {
                Map wd = new HashMap();
                wd.put("DOC_ID", rs.getString(1));
                wd.put("CREATE_TIME", rs.getDate(2));
                wd.put("EDIT_TIME", rs.getDate(3));
                wd.put("AUTHOR_ID", rs.getString(4));
                wd.put("EDIT_ID", rs.getString(5));
                wd.put("TITLE", rs.getString(6));
                wd.put("STATUS", rs.getString(7));
 
                wd.put("CONTENT", rs.getString(8));
                wd.put("TYPE_ID", rs.getString(9));
                wd.put("ABSTRACT", rs.getString(10));
                wdList.add(wd);
            }
 
 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wdList;
    }
 
    public String getStringFromList(List list) {
        String result = "";
 
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            result += "@@@@@@@" + BeansHelp.convert(map.get("DOC_ID").toString()) + "#######";
            result += BeansHelp.convert(map.get("CREATE_TIME") == null ? "null" : map.get("CREATE_TIME").toString()) + "#######";
 
            result += BeansHelp.convert(map.get("EDIT_TIME") == null ? "null" : map.get("EDIT_TIME").toString()) + "#######";
            result += BeansHelp.convert(map.get("AUTHOR_ID") == null ? "null" : map.get("AUTHOR_ID").toString()) + "#######";
            result += BeansHelp.convert(map.get("TITLE") == null ? "null" : map.get("TITLE").toString()) + "#######";
            result += BeansHelp.convert(map.get("STATUS") == null ? "null" : map.get("STATUS").toString()) + "#######";
            result += BeansHelp.convert(map.get("CONTENT") == null ? "null" : map.get("CONTENT").toString()) + "#######";
            result += BeansHelp.convert(map.get("TYPE_ID") == null ? "null" : map.get("TYPE_ID").toString()) + "#######";
            result += BeansHelp.convert(map.get("ABSTRACT") == null ? "null" : map.get("ABSTRACT").toString()).replaceAll("\n", " ") + "\n";
        }
 
        return result;
    }
 
    public void writeTo(String filepath) {
        int len = this.getCountOfWD();
        Page page = new Page();
        File f = new File(filepath);
        File dir = new File(filepath.substring(0, filepath.lastIndexOf("/")));
        try {
            if (!dir.exists()) dir.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        try {
            if (!f.exists())
                f.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        PrintWriter out = null;
        try {
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath), "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        int count = len / size + 1;
        System.out.println("共 " + count + " 页");
        for (int i = 1495; i < count; i++) {
            int start = i * size;
            int end = (i + 1) * size;
            if (end > len) end = len;
            else ;
 
            System.out.println("第 " + i + "页  " + "第 " + start + " 条记录到第" + end + " 条记录");
            page.set(start, end);
            String content = page.getStringFromList(page.getNextPage());
            out.print(content);
 
        }
 
 
        out.flush();
        out.close();
    }
 
    public void writeToIndex(String indexPath) {
 
        int len = this.getCountOfWD();
        Page page = new Page();
        SimpleIndexWriter siw = null;
        try {
            siw = new SimpleIndexWriter(indexPath);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Date sta = new Date();
        int count = len / size + 1;
        System.out.println("共 " + count + " 页");
        for (int i = 0; i < 188; i++) {
            int start = i * size;
            int end = (i + 1) * size;
            if (end > len) end = len;
            else ;
 
            System.out.println("第 " + i + "页  " + "第 " + start + " 条记录到第" + end + " 条记录");
            page.set(start, end);
            List list = page.getNextPage();
            for (int j = 0; j < list.size(); j++) {
                Map docMap = (Map) list.get(j);
                siw.writeNewIndex(siw.getDocumentFromMap(docMap));
            }
 
        }
        siw.closeIndex();
        Date sto = new Date();
 
        System.out.println("用时:" + (sta.getTime() - sto.getTime()) + "毫秒");
    }
 
    public void showPage(String command) throws SQLException {
        String sql = "select wd_id,wd_bt,rnum from(select wd_id,wd_bt,rownum rnum from nrgl_wd)" +
                "where rnum between ? and ?";
        ps = con.prepareStatement(sql);
        if (command.equals("up")) {
            if (start - size <= 1) {
                System.out.println("已经是第一页了");
                start = 1;
                end = 5;
                ps.setInt(1, start);
                ps.setInt(2, end);
                rs = ps.executeQuery();
                jd.printResultSet(rs);
            } else {
                start = start - size;
                end = start + size - 1;
                ps.setInt(1, start);
                ps.setInt(2, end);
                rs = ps.executeQuery();
                jd.printResultSet(rs);
            }
 
        } else if (command.equals("next")) {
            if (count - start <= size) {
                System.out.println("已经是最后一页");
                end = count;
                start = end - count % size + 1;
                ps.setInt(1, start);
                ps.setInt(2, end);
                rs = ps.executeQuery();
                jd.printResultSet(rs);
            } else {
                start = start + size;
                end = end + size;
                ps.setInt(1, start);
                ps.setInt(2, end);
                rs = ps.executeQuery();
                jd.printResultSet(rs);
            }
        } else if (command.equals("exit")) {
            System.exit(-1);
        }
    }
 
 
    public static void main(String[] args) throws SQLException {
        Page p = new Page();
        p.writeToIndex("D:/baIndex/");
 
//
 
    }
}